package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ConstantItem;
import com.ruoyi.system.service.IConstantItemService;
import com.ruoyi.system.service.impl.ConstantTypeServiceImpl;
import com.ruoyi.web.controller.tool.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 常数项Controller
 *
 * @author ruoyi
 * @date 2023-07-04
 */
@Api(tags = "常数项模块")
@RestController
@RequestMapping("/system/ConstantItem")
public class ConstantItemController extends BaseController {
    @Autowired
    private IConstantItemService constantItemService;

    private class Wrap implements Serializable {
        ConstantItem ConstantItem;
        String ConstantTypeId;

        @JsonProperty("ConstantItem")
        public com.ruoyi.system.domain.ConstantItem getConstantItem() {
            return ConstantItem;
        }

        @JsonProperty("ConstantTypeId")
        public String getConstantTypeId() {
            return ConstantTypeId;
        }
    }

    @Autowired
    ConstantTypeServiceImpl constantTypeService;

    private Wrap convert(ConstantItem origin) {
        Wrap tempWrap = new Wrap();
        tempWrap.ConstantTypeId = constantTypeService.getById(origin.getConstantTypeId()).getConstantTypeName();
        tempWrap.ConstantItem = origin;
        return tempWrap;
    }

    /**
     * 查询常数项列表
     */
    @ApiOperation(value = "查询常数项列表")
    @PreAuthorize("@ss.hasPermi('system:ConstantItem:list')")
    @GetMapping("/list")
    public Page<Wrap> list(ConstantItem entity, Page<ConstantItem> page) {

        LambdaQueryWrapper<ConstantItem> lambdaQueryWrapper = new LambdaQueryWrapper<ConstantItem>();
        Page<ConstantItem> old = constantItemService.page(page, lambdaQueryWrapper);
        Page<Wrap> pageWrap = new Page<>();
        List<Wrap> wraps = new ArrayList<>();
        List<ConstantItem> object = old.getRecords();
        for (ConstantItem item : object) {
            wraps.add(convert(item));
        }
        pageWrap.setRecords(wraps);
        PageUtil pageUtil = new PageUtil();
        pageUtil.setPage(pageWrap, old);
        return pageWrap;
    }

    /**
     * 导出常数项列表
     */
    @ApiOperation(value = "导出常数项列表")
    @PreAuthorize("@ss.hasPermi('system:ConstantItem:export')")
    @Log(title = "常数项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ConstantItem constantItem) {
        LambdaQueryWrapper<ConstantItem> queryWrapper = new LambdaQueryWrapper<>();
        List<ConstantItem> list = constantItemService.list(queryWrapper);
        ExcelUtil<ConstantItem> util = new ExcelUtil<ConstantItem>(ConstantItem.class);
        util.exportExcel(response, list, "常数项数据");
    }


    /**
     * 获取常数项详细信息
     */
    @ApiOperation(value = "获取常数项详细信息")
    @PreAuthorize("@ss.hasPermi('system:ConstantItem:query')")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(constantItemService.getById(id));
    }

    /**
     * 新增常数项
     */
    @ApiOperation(value = "新增常数项")
    @PreAuthorize("@ss.hasPermi('system:ConstantItem:add')")
    @Log(title = "常数项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ConstantItem constantItem) {
        return toAjax(constantItemService.save(constantItem));
    }

    /**
     * 修改常数项
     */
    @ApiOperation(value = "修改常数项")
    @PreAuthorize("@ss.hasPermi('system:ConstantItem:edit')")
    @Log(title = "常数项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ConstantItem constantItem) {
        return toAjax(constantItemService.updateById(constantItem));
    }


    /**
     * 删除常数项
     */
    @ApiOperation(value = "删除常数项")
    @PreAuthorize("@ss.hasPermi('system:ConstantItem:remove')")
    @Log(title = "常数项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids) {
        return toAjax(constantItemService.removeBatchByIds(Arrays.asList(Ids)));
    }
}
